Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
how to parse "" to long("0") in dotnet

given the following code:

string example = "1234";
long parsed_example = long.Parse(example);
Console.Writeline(parsed_example);
# => 1234

Works great.

the following example does not:

string example = "";
long parsed_example = long.Parse(example);
# [System.FormatException: Input string was not in a correct format.]

However the goal is:

string example = "";
if (example == "")
{
    example = "0";
}
long parsed_example = long.Parse(example);
Console.Writeline(parsed_example);
# => 0

is there a shorter, apropriate solution? The above code would almost justify a tiny function, Id preferable have a inline solution. Maybe something such as (pseudo code):

string example = "";
long parsed_example = example ?? 0, long.Parse(example);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

long parsed_example = example == "" ? 0 : long.Parse(example);

However: don't be obsessed with single-line solutions; a multi-line solution is often more readable and correct. There are no prizes for creating complex code. You may also wish to look at string.IsNullOrWhiteSpace, long.TryParse, etc. For example:

long value;
if (string.IsNullOrWhiteSpace(example))
{
    // what you want to do with blank/empty values
    value = 42;
}
else if (!long.TryParse(example, out value))
{
    // what you want to do with non-integer values 
   value = 84;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!